<?php
include '../../../wp-blog-header.php';
require_once('rss_fetch.inc');
$url = 'http://feed43.com/3106820561815274.xml';
$brand_id = '196';
$rss = fetch_rss($url);
//echo "<pre>";
//print_r($rss->items);
//exit();

if(!empty($rss->items) && is_array($rss->items))
{
  foreach($rss->items as $key=>$value)
  {
    $deal_link = $value['link'];
    $fetched_values = explode('~',$value['description']);
    $final_values = explode('$',$fetched_values['1']);
    
    /*
    echo '<pre>';
    print_r($final_values);
    echo '</pre>';
    */
    
    $columns_deal['name'] = pods_sanitize(ucwords(strtolower($final_values['1'])));
    $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
    $ext_img_url = $final_values['0'];
    
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" OR (t.ext_image_url = "'.$ext_img_url.'" AND t.daily = "1")';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    if($dealsFound == 0)
    {
      $columns_deal['city'] = '26';//Goa
      
      $shortened_url = make_bitly_url($deal_link);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($deal_link);
      }
      $columns_deal['extlink'] = $shortened_url;
      
      $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime('+1 day',strtotime(date("Y-m-d 06:30:00"))));
      $columns_deal['camp_start'] = date('Y-m-d H:i:s');
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      $columns_deal['oriprice'] = $final_values['2'];
      
      $discount = explode('%',$final_values['3']);
    
      $columns_deal['disper'] = $discount['0'];
      $columns_deal['disprice'] = ($final_values['2'] - $final_values['4']);
      $columns_deal['ext_image_url'] = $ext_img_url;
    
      $cat_id = check_daily_category($columns_deal['name']);
      if(!empty($cat_id))
      {
        $columns_deal['camp_cat'] = $cat_id;
      }
    
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
      /*
      echo '<pre>';
      print_r($params);
      echo '</pre>';
      */
      
      $api->save_pod_item($params);
    }
    
  }
}
?>




